<?php

namespace api\ygzc\controller;

use think\facade\Db;
use think\facade\Validate;
use api\ygzc\model\GoodsModel;
use api\ygzc\model\GoodsCategoryModel;
use cmf\controller\RestBaseController;

class GoodsController extends RestBaseController
{
    
	protected $code = 'ZF00004';
	// 初始化
    public function initialize()
    {
		if ($this->request->isPost()) {
            $validate = new \think\Validate([
                'username' => 'require',
                'password' => 'require',
				'domain' => 'require',
            ]);
            $validate->message([
                'username.require' 	=> '授权账号不能为空',
                'password.require' 	=> '密码不能为空',
				'domain.require' 	=> '域名不能为空',
            ]);
            $data = $this->request->post();
            if (!$validate->check($data)) {
                $this->error($validate->getError());
            }
			$find_merchant = Db::name('merchant')->where('user_login',$data['username'])->find();
			if($find_merchant){
				$comparePasswordResult = cmf_compare_password($data['password'], $find_merchant['user_pass']);
				if ($comparePasswordResult) {
					//拉黑判断。
					if ($find_merchant['user_status'] == 0) {
						$this->error('授权账号被禁用');
					}
					$ip =  get_client_ip(0, true);
					if ($find_merchant['user_ip'] != $ip) {
						$this->error('账号授权IP不正确');
					}
					if ($find_merchant['user_domain'] != $data['domain']) {
						$this->error('账号授权域名不正确');
					}
					$cur_time = time();
					if($cur_time < $find_merchant['start_time'] || $cur_time > $find_merchant['end_time']){
						$this->error('授权账号不在授权时间内');
					}
					$this->add_action_log($find_merchant['id'],$data,$ip); 
				}else{
					$this->error("授权密码不正确");
				}
			}else{
				$this->error("授权账号不存在");
			}
		}else{
			$this->error("请求错误");
		}
    }
	
	
	//获取商品比价
	public function get_other_merchant_list()
	{
		$param = $this->request->param();
		$goods_id = $param['zc_goods_id'];
		$url = "http://ggzyjyzx.shandong.gov.cn/wssc/goods/$goods_id/aggregation?from=mall&pid=$goods_id";
		$res = $this->get_url($url);
		$data = json_decode($res,true);
		$this->success('请求成功',$data);
	}
	
	//获取商品分类
	public function get_cate_list()
	{
		$param = $this->request->param();
		$GoodsCategoryModel = new GoodsCategoryModel();
		$categoryTree = $GoodsCategoryModel->CategoryTree();
		$this->success('请求成功',$categoryTree);
	}
	
	
	//获取商品品牌
	public function get_brand_list()
	{
		$param = $this->request->param();
		$where = [];
		if(!empty($param['cate_id'])){
			$where[] = ['cate_id','=',$param['cate_id']];
		}
		$list = Db::name('goods_brand')->where($where)->select();
		$this->success('请求成功',$list);
	}
	
	//获取商品列表
	public function get_goods_list()
	{
		$param = $this->request->param();
		$where = [];
		if(!empty($param['cate_id'])){
			$where[] = ['cate_id','=',$param['cate_id']];
		}
		if(!empty($param['brand_id'])){
			$where[] = ['brand_id','=',$param['brand_id']];
		}
		if(!empty($param['title'])){
			$where[] = ['post_title','like',"%".$param['title']."%"];
		}
		$page = $param['page'];
		$page_num = $param['pageNum'];
		$GoodsModel = new GoodsModel();
		$total = $GoodsModel->where($where)->count(); 
		$list = $GoodsModel->with('cate')->where($where)->page($page,$page_num)->order('id desc')->select();
		$this->success('请求成功',['list'=>$list,'total'=>$total]);
	} 
	
	//获取商品详情
	public function get_goods_detail()
	{
		$param = $this->request->param();
		$id = $param['id'];
		$GoodsModel = new GoodsModel();
		$data = $GoodsModel->with('cate')->find($id);
		if($data){
			$this->success('请求成功',$data);
		}else{
			$this->error('请求错误'); 
		}
		
	}
	
	
	function get_url($url) {
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_HTTPGET, true);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //TRUE 将curl_exec()获取的信息以字符串返回，而不是直接输出。
		$header = [
		'Content-Type: application/json;charset=UTF-8',
		'siteCode: sdszfcg'
		]; //设置一个你的浏览器agent的header
		curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
		curl_setopt($ch, CURLOPT_HEADER, 0); //返回response头部信息
		curl_setopt($ch, CURLINFO_HEADER_OUT, true); //TRUE 时追踪句柄的请求字符串，从 PHP 5.1.3 开始可用。这个很关键，就是允许你查看请求header
		curl_setopt($ch, CURLOPT_URL, $url);
		$result = curl_exec($ch);
		curl_close($ch);
		return $result;
	}
	
}
